local gamelib = require "club.threecard.gamelib"
local slotlib = require "club.threecard.slotlib"
local components = slotlib.require "components"

local slot_system = slotlib.systems.slot_system
local class = gamelib.entitas.class
local Matcher = gamelib.entitas.Matcher
local GroupEvent = gamelib.entitas.GroupEvent

local M = class("blaze_win_system", slot_system)

function M:ctor(context, ingame)
    M.super.ctor(self, context, ingame)
    self.blaze_win = slotlib.require("widget.BlazeWin").new()
end

function M:initialize()
    self.blaze_win:load()
    local fx_root = self.ingame.fx_root
    self.context.machine:replace(components.blaze_win_component, 0, fx_root)
end

function M:get_trigger()
    return {{Matcher({components.blaze_win_component, components.payout_component}), GroupEvent.ADDED}}
end

function M:filter(entity)
    return entity:has(components.blaze_win_component) and entity:has(components.payout_component)
end

function M:execute(entitas)
    if entitas:size() ~= 1 then
        gamelib.log.error("数据错误，结果组件不可能~=1->"..entitas:size())
        return
    end

    entitas:foreach(function(e)
        local component = e:get(components.blaze_win_component)
        local multiplied = component.multiplied
        if multiplied <= 2 then
            return
        end
        local payout = e:get(components.payout_component).payout
        local time = self.blaze_win:play(payout, self.blaze_win.clips.blaze_win, component.parent)
        self:wait(time)
        self.blaze_win:close()
    end)
end

function M:skip()
    self.super.skip(self)
    self.blaze_win:close()
end

function M:tear_down()
    self.blaze_win:hide()
end

return M